package com.mockCommon;

import java.net.URI;
import java.net.URISyntaxException;

import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.NullProgressMonitor;
import com.atlassian.jira.rest.client.domain.Issue;
import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;

/**
 * 一个简单的 jira 客户端
 * 
 * @author xuqinghua
 *
 */
public class SimpleJiraClient {

	/**
	 * 
	 * @param jiraUrl
	 * @param username
	 * @param password
	 */
	public SimpleJiraClient(final String jiraUrl, final String username, final String password) {
		this.jiraUrl = jiraUrl;
		this.username = username;
		this.password = password;
		try {
			this.client = this.login_jira(username, password);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}
	
	/**
    * 获取并返回指定的Issue对象
    * 
    * @param issueNum
    * @param username
    * @param password
    * @return
    * @throws URISyntaxException
    */
   public Issue get_issue(String issueNum) throws URISyntaxException {
       try {
           final NullProgressMonitor pm = new NullProgressMonitor();
           final Issue issue = this.client.getIssueClient().getIssue(issueNum, pm);
           return issue;
       } catch (Exception e) {
           e.printStackTrace();
       }
       return null;
   }

	/**
	 * 登录JIRA并返回指定的JiraRestClient对象
	 * 
	 * @param username
	 * @param password
	 * @return
	 * @throws URISyntaxException
	 */
	public JiraRestClient login_jira(String username, String password) throws URISyntaxException {
		try {
			final JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
			final URI jiraServerUri = new URI(jiraUrl);
			final JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, this.username,
					this.password);
			return restClient;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	 */
	public void create_issue() {
		//
		
	}

	private JiraRestClient client;
	private String username = "gbench";
	private String password = "pi31415926";
	private String jiraUrl = "http://localhost:8080/";

}
